﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.APP.Classes;
//using _WebShared.Classes;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class MP_Jobsites : System.Web.UI.UserControl
    {
        private SsMng ssMng = new SsMng();
        private IJobSiteBLL jobsiteBLL = new JobSiteBLL();
        private IWorkAreaBLL areaBLL = new WorkAreaBLL();
        private IJobSiteLevelBLL levelBLL = new JobsiteLevelBLL();
        private IJobSiteCommandBLL cmd = new JobSiteCommandBLL();
        private IPositionBLL positionBLL = new PositionBLL();
        private IMasDistrictBLL districtBLL = new MasDistrictBLL();
        private IMasCountryBLL countryBLL = new MasCountryBLL();
        private IMasCityBLL cityBLL = new MasCityBLL();
        private DropDownListBLL ddlBLL = new DropDownListBLL();

        private MasCountryInfo countryInfo = new MasCountryInfo();
        private JobSiteInfo jsInfo = new JobSiteInfo();
        private object _dataItem = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            txtPostalCode.Attributes.Add("onkeypress", "return isNumberKey(event)");

            if (DataItem != null)
            {
                LoadDDLJobsiteUpperSeq();
                LoadDDLJobsiteExternalSeq();
                LoadDDLWorkAreaSeq();
                LoadDDLJobsiteLevelSeq();
                LoadDDLCountrySeq();
                LoadDDLJobsiteHeadPosSeq();
                ddlJobSiteRef.Visible = Request.QueryString["org"].ToString() == SystemInfo._JobsiteInternal;
                lblJobsiteRef.Visible = ddlJobSiteRef.Visible;
                reqddlJobSiteRef.Enabled = ddlJobSiteRef.Visible;
                string JobSeq = Session["TreeNodeValue"].ToString();

                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
                    //if (DataBinder.Eval(DataItem, "work_area_seq") != null)
                    ddlWorkAreaSeq.SelectedValue = Convert.ToString(DataBinder.Eval(DataItem, "work_area_seq"));
                    ddlJobsiteUpperSeq.SelectedValue = Convert.ToString(DataBinder.Eval(DataItem, "upper_job_site_seq"));
                    if (ddlJobSiteRef.Visible)
                        ddlJobSiteRef.SelectedValue = Convert.ToString(DataBinder.Eval(DataItem, "ref_job_site_seq"));
                    ddlJobsiteLevelSeq.SelectedValue = Convert.ToString(DataBinder.Eval(DataItem, "job_site_level_seq"));
                    ddlCountrySeq.SelectedValue = Convert.ToString(DataBinder.Eval(DataItem, "country_seq"));
                    LoadDDLCountryChange(ddlCountrySeq.SelectedValue);
                    LoadDDLProvinceSeq(ddlCountrySeq.SelectedValue);

                    if (DataBinder.Eval(DataItem, "prov_seq").ToString().Length > 0)
                    {
                        cmbProvince.SelectedValue = Convert.ToString(DataBinder.Eval(DataItem, "prov_seq"));
                        LoadDDLDistrictSeq(cmbProvince.SelectedValue);
                        ddlDistrictSeq.SelectedValue = Convert.ToString(DataBinder.Eval(DataItem, "district_seq"));
                    }
                }
                else
                {
                    if (Convert.ToInt32(JobSeq) > 0)
                    {
                        IList<JobSiteInfo> jobsiteInfo = jobsiteBLL.ListJobSiteBySequence(JobSeq);

                        if (jobsiteInfo.Count > 0)
                        {
                            ddlJobsiteUpperSeq.SelectedValue = JobSeq;
                            ddlJobsiteUpperSeq.Enabled = false;
                            txtJobsiteRef.Text = jobsiteInfo[0].RefNo;
                        }
                    }
                    ddlCountrySeq.SelectedValue = SystemInfo._Local;
                    LoadDDLCountryChange(ddlCountrySeq.SelectedValue);
                }
            }
        }

        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }

        protected void LoadDDLJobsiteUpperSeq()
        {
            IList<JobSiteInfo> InfoList = jobsiteBLL.ListJobSiteByTypes(Request.QueryString["org"]);
            ddlJobsiteUpperSeq.DataSource = InfoList; // job.SelectDropDownList(strSystemJobsiteUpperSeq, strIsInternal);//ไปเอาหน่วยงานขึ้นตรงของเจ้าของระบบ จะไม่ error
            ddlJobsiteUpperSeq.DataBind();
        }
        protected void LoadDDLJobsiteExternalSeq()
        {
            IList<JobSiteInfo> InfoList = jobsiteBLL.ListJobSiteByTypes(SystemInfo._JobsiteExternal);
            ddlJobSiteRef.DataSource = InfoList;
            ddlJobSiteRef.DataBind();
        }

        protected void LoadDDLWorkAreaSeq()
        {
            IList<WorkAreaInfo> Info = areaBLL.ListWorkAreas(null);
            ddlWorkAreaSeq.DataSource = Info;
            ddlWorkAreaSeq.DataBind();
        }

        protected void LoadDDLJobsiteLevelSeq()
        {
            IList<DropDownListInfo> infoList = ddlBLL.LoadListJobsiteLevels();
            ddlJobsiteLevelSeq.DataSource = infoList;
            ddlJobsiteLevelSeq.DataBind();
        }
        //protected void LoadDDLZoneSeq()
        //{
        //    IList<DropDownListInfo> infoList = ddlBLL.LoadListCountryZones();
        //    cmbZone.DataSource = infoList;
        //}
        protected void LoadDDLCountrySeq()
        {
            IList<DropDownListInfo> infoList = ddlBLL.LoadListCountry();
            ddlCountrySeq.DataSource = infoList;
            ddlCountrySeq.DataBind();
        }
        protected void LoadDDLJobsiteHeadPosSeq()
        {
            IList<PositionInfo> infoList = positionBLL.ListPositions();
            ddlJobSiteHeadPosSeq.DataSource = infoList;
            ddlJobSiteHeadPosSeq.DataBind();
        }
        protected void LoadDDLProvinceSeq(string Country)
        {
            cmbProvince.Items.Clear();

            if (Country == SystemInfo._Local)
            {
                IList<DropDownListInfo> infoList = ddlBLL.LoadListProvinces(null);
                cmbProvince.DataSource = infoList;
            }
            else
            {
                IList<MasCityInfo> cityList = cityBLL.ListMasCities("", Country);
                cmbProvince.DataSource = cityList;
            }
            
            cmbProvince.DataBind();
        }
        protected void LoadDDLDistrictSeq(string provSeq)
        {
            IList<MasDistrictInfo> infoList = districtBLL.ListDistricts("", "", provSeq);
            IList<MasDistrictInfo> List = new List<MasDistrictInfo>();

            for (int i = 0; i < infoList.Count; i++)
            {
                MasDistrictInfo c = new MasDistrictInfo();
                c.Sequence = infoList[i].Sequence;
                c.Name = "ตำบล" + infoList[i].Name + " อำเภอ" + infoList[i].Amphurs.Name;

                List.Add(c);
            }
            ddlDistrictSeq.DataSource = List;
            ddlDistrictSeq.DataBind();
        }
        

        //============================= ControlEvents ======================================

        protected void cmdAdd_Click(object sender, EventArgs e)
        {
            jobsiteBLL.Insert(txtJobsiteRef.Text.Trim()
                , txtJobsiteName.Text
                , txtJobsiteNameShort.Text
                , txtJobsiteNameEng.Text
                , txtNameEngShort.Text
                , txtJobsiteGFMIS.Text.Trim()
                , txtJobDescription.Text
                , txtRemarks.Text
                , ssMng.EmpSeqI
                , txtDateStart.GetStringValue().Length > 0 ? DateTime.Parse(txtDateStart.GetStringValue()) : (DateTime?)null
                , null
                , txtAddress.Text
                , ddlDistrictSeq.SelectedValue//.Length > 0 ? Int32.Parse(ddlDistrictSeq.SelectedValue) : (int?)null
                , (ddlCountrySeq.SelectedValue == SystemInfo._Local ? "" : cmbProvince.SelectedValue)//.Length > 0 ? Int32.Parse(cmbProvince.SelectedValue) : (int?)null
                , ddlCountrySeq.SelectedValue
                , txtPostalCode.Text.Trim()
                , txtTel.Text.Trim()
                , txtFax.Text.Trim()
                , ddlJobsiteUpperSeq.SelectedValue.Length > 0 ? Int32.Parse(ddlJobsiteUpperSeq.SelectedValue) : (int?)null
                , Int32.Parse(Request.QueryString["org"])
                , Int32.Parse(ddlWorkAreaSeq.SelectedValue)
                , Int32.Parse(ddlJobsiteLevelSeq.SelectedValue)
                , ddlJobSiteRef.SelectedValue.Length > 0 ? Int32.Parse(ddlJobSiteRef.SelectedValue) : (int?)null
				, txtAddress2.Text.Trim()
                , txtWebsite.Text.Trim()
                , ddlJobSiteHeadPosSeq.SelectedValue
                );
        }

        protected void cmdUpdate_Click(object sender, EventArgs e)
        {
            jobsiteBLL.Update(Int32.Parse(lblJobsiteSeq.Text.Trim())
                , txtJobsiteRef.Text.Trim()
                , txtJobsiteName.Text
                , txtJobsiteNameShort.Text
                , txtJobsiteNameEng.Text
                , txtNameEngShort.Text
                , txtJobsiteGFMIS.Text.Trim()
                , txtJobDescription.Text
                , txtRemarks.Text
                , ssMng.EmpSeqI
                , txtDateStart.GetStringValue().Length > 0 ? DateTime.Parse(txtDateStart.GetStringValue()) : (DateTime?)null
                , null
                , txtAddress.Text
                , ddlDistrictSeq.SelectedValue//.Length > 0 ? Int32.Parse(ddlDistrictSeq.SelectedValue) : (int?)null
                , (ddlCountrySeq.SelectedValue == SystemInfo._Local ? "" : cmbProvince.SelectedValue)
                , ddlCountrySeq.SelectedValue
                , txtPostalCode.Text.Trim()
                , txtTel.Text.Trim()
                , txtFax.Text.Trim()
                , ddlJobsiteUpperSeq.SelectedValue.Length > 0 ? Int32.Parse(ddlJobsiteUpperSeq.SelectedValue) : (int?)null
                , Int32.Parse(Request.QueryString["org"])
                , Int32.Parse(ddlWorkAreaSeq.SelectedValue)
                , Int32.Parse(ddlJobsiteLevelSeq.SelectedValue)
                , ddlJobSiteRef.SelectedValue.Length > 0 ? Int32.Parse(ddlJobSiteRef.SelectedValue) : (int?)null
				, txtAddress2.Text.Trim()
                , txtWebsite.Text.Trim()
                , ddlJobSiteHeadPosSeq.SelectedValue);
        }

        protected void CheckDateValid(object source, ServerValidateEventArgs args)
        {
            //if (!DateTimeX.IsValidDmyDate(args.Value, true))
            //{
            //    args.IsValid = false;
            //}
        }

        protected void CheckPostalCode(object source, ServerValidateEventArgs args)
        {
            if (args.Value.Length > 0 && args.Value.Length < 5)
            {
                args.IsValid = false;
            }
        }
        protected void LoadDDLCountryChange(string CountrySeq)
        {
            if (CountrySeq.Length > 0)
            {
                //IList<MasCountryInfo> countryList = countryBLL.ListMasCountries(CountrySeq, "");
                //if (countryInfo.CountryZones != null)
                //    cmbZone.SelectedValue = countryList[0].Sequence.ToString();

                //LoadDDLProvinceSeq(CountrySeq);
                lblProvinceCity.Text = CountrySeq.ToString() == SystemInfo._Local ? "จังหวัด" : "เมือง";
            }
        }
        protected void ddlCountrySeq_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadDDLCountryChange(ddlCountrySeq.SelectedValue);
        }

        //protected void cmbProvince_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        //{
        //    if (cmbProvince.SelectedValue.Length > 0 && lblProvinceCity.Text == "จังหวัด")
        //    {
        //        ddlDistrictSeq.Enabled = true;
        //        LoadDDLDistrictSeq(cmbProvince.SelectedValue);
        //    }
        //    else
        //        ddlDistrictSeq.Enabled = false;
        //}

        protected void cmbProvince_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            LoadDDLProvinceSeq(ddlCountrySeq.SelectedValue);
        }

        protected void ddlDistrictSeq_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            LoadDDLDistrictSeq(cmbProvince.SelectedValue);
        }
    }
}